// ==++==
//
//   
//    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
//   
//    The use and distribution terms for this software are contained in the file
//    named license.txt, which can be found in the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by the
//    terms of this license.
//   
//    You must not remove this notice, or any other, from this software.
//   
//
// ==--==
using System.IO;
using System;
class Co6001FromString {
 public static String s_strActiveBugNums = "";
 public static String s_strDtTmVer       = "";
 public static String s_strLastModCoder  = "";
 public static String s_strOrigCoder     = "";
 public static String s_strComponentBeingTested
   = "Byte.Parse(String)";
 public static String s_strTFName        = "Co6001FromString.cs";
 public static String s_strTFAbbrev      = "Co6001";
 public static String s_strTFPath        = "";
 public Boolean m_bVerbose                     = false;    
 public void Debug( String arg ) {
 if (m_bVerbose) Console.WriteLine( "Verbose:" + arg );
 }
 public Boolean runTest() {
 int iCountErrors = 0;
 int iCountTestcases = 0;
 String strLoc="...";
 int iCount = 0;
 Byte SByteTest = new Byte();
 Console.WriteLine( s_strTFPath +" "+ s_strTFName +" ,for "+ s_strComponentBeingTested +"  ,Source ver "+ s_strDtTmVer );
 Debug( "On" );
 try {
 strLoc="Loc001:RoundTrip";
 Debug( strLoc + " started.");
 try {
 for( iCount = Convert.ToInt32( Byte.MinValue ); iCount <= Convert.ToInt32( Byte.MaxValue ); iCount++) {
 iCountTestcases++;
 SByteTest = Byte.Parse( ((Byte)Convert.ToChar( iCount )).ToString((IFormatProvider)null) );  
 if ( SByteTest != Convert.ToByte(iCount) ) {
 iCountErrors++;
 Debug( "Round Trip: " + iCount + " ==> " + SByteTest );
 }
 }
 } catch( Exception e ) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 Debug( strLoc + " ended. Testcases = " + iCountTestcases + " Errors = " + iCountErrors );
 strLoc="Loc002:WhiteSpace";
 Debug( strLoc + " started.");
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse( "    " + Convert.ToByte( 65 ) );
 if ( SByteTest != Convert.ToByte( 65 ) ) {
 iCountErrors++;
 Debug( "'" + Convert.ToChar(SByteTest) + "'" );
 }
 iCountTestcases++;
 SByteTest = Byte.Parse( "    " + Convert.ToByte( 66 ) + "    " );
 if ( SByteTest != Convert.ToByte( 66 ) ) {
 iCountErrors++;
 Debug( "'" + Convert.ToChar(SByteTest) + "'" );
 }
 iCountTestcases++;
 SByteTest = Byte.Parse( Convert.ToByte( 67 ) + "    ");
 if ( SByteTest != Convert.ToByte( 67 ) ) {
 iCountErrors++;
 Debug( "'" + Convert.ToChar(SByteTest) + "'" );
 }
 } catch( Exception e ) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 Debug( strLoc + " ended. Testcases = " + iCountTestcases + " Errors = " + iCountErrors );
 strLoc="Loc003:Exception";
 Debug( strLoc + "started.");
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse(null);
 iCountErrors++;
 Debug( "No ArgumentException thrown for null");
 } catch( ArgumentNullException ) {}
 catch( Exception e) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse( "text" );
 iCountErrors++;
 Debug( "No FormatException thrown for 'text'" );
 } catch( FormatException ) {}
 catch( Exception e ) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse( "3.14" );
 iCountErrors++;
 Debug( "No FormatException thrown for '3.14'" );
 } catch( FormatException ) {}
 catch( Exception e) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse( Convert.ToString(Byte.MinValue - Convert.ToByte(1)) );
 iCountErrors++;
 Debug( "No Overflow generated by: Byte.MinValue - Convert.ToByte(1)" );
 } catch( OverflowException ) {}
 catch( Exception e ) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 try {
 iCountTestcases++;
 SByteTest = Byte.Parse( Convert.ToString(Byte.MaxValue + Convert.ToByte(1)) );
 iCountErrors++;
 Debug( "No Overflow generated by: Byte.MaxValue + Convert.ToByte(1)" );
 } catch( OverflowException ) {}
 catch( Exception e ) {
 Console.WriteLine( strLoc + ":" + e + ", iCount = " + iCount + ", SByteTest = " + SByteTest );
 }
 Debug( strLoc + " ended. Testcases = " + iCountTestcases + " Errors = " + iCountErrors );
 }
 catch( Exception exc_general )
   {
   ++iCountErrors;
   Console.WriteLine( "Error Err_8888yyy ("+ s_strTFAbbrev +")!  Unexpected exception"+
		      " thrown sometime after strLoc=="+ strLoc +" ,exc_general=="+ exc_general );
   }
 if ( iCountErrors == 0 )
   {
   Console.WriteLine( "paSs.   "+ s_strTFPath +" "+ s_strTFName +
		      "  ,iCountTestcases=="+ iCountTestcases );
   return true;
   }
 else
   {
   Console.WriteLine( "FAiL!   "+ s_strTFPath +" "+ s_strTFName +
		      "  ,iCountErrors=="+ iCountErrors +
		      " ,BugNums?: "+ s_strActiveBugNums );
   return false;
   }
 }
 public static void Main( String[] args ) { 
 Environment.ExitCode = 1;  
 Boolean bResult = false; 
 Co6001FromString cbX = new Co6001FromString();
 try {
 if ( args[0].Equals("-v" ) == true ){
 Console.WriteLine( "Activating Verbose Mode...." );
 cbX.m_bVerbose = true;
 }
 } catch( Exception ) {}
 try
   {
   bResult = cbX.runTest();
   }
 catch ( Exception exc_main )
   {
   bResult = false;
   Console.WriteLine( "FAiL!  Error Err_9999 ("+ s_strTFAbbrev +")!" +
		      "Uncaught Exception caught fell to Main(), "+
		      "exec = "+ exc_main );
   }
 if ( ! bResult )
   {
   Console.WriteLine( "Try using '-v' flag" );
   Console.WriteLine( s_strTFPath + s_strTFName );
   Console.WriteLine( " " );
   Console.WriteLine( "FAiL!  "+ s_strTFAbbrev );  
   Console.WriteLine( " " );
   }
 if ( bResult == true ) Environment.ExitCode = 0; else Environment.ExitCode = 1; 
 }
}
